package chkui.springcore.example.javabase.conversion.entity;

import java.lang.reflect.Field;

public abstract class Device {
	abstract String getName();

	/**
	 * 字符串格式:cpu=amd;ram=kingston;graphic=Navidia;
	 * 
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	public void pares(String text){
		Field[] fields = this.getClass().getDeclaredFields();
		for (Field field : fields) {
			int begIndex = text.indexOf(field.getName());
			int endIndex = text.indexOf(";", begIndex);
			String sub = text.substring(begIndex, endIndex), value = sub.split("=")[1];
			field.setAccessible(true);
			try {
				field.set(this, value);
			} catch (IllegalArgumentException | IllegalAccessException e) {
				throw new RuntimeException(e);
			}
		}
	};

	public String value(){
		Field[] fields = this.getClass().getDeclaredFields();
		StringBuilder sb = new StringBuilder();
		for (Field field : fields) {
			sb.append(field.getName());
			sb.append("=");
			try {
				
				sb.append(field.get(this).toString());
			} catch (IllegalArgumentException | IllegalAccessException e) {
				throw new RuntimeException(e);
			}
			sb.append(";");
		}
		return sb.toString();
	}
}
